package com.wisemoresystem.seal.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.task.Task;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_check;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_push;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印章下发
 * @author hxj
 *
 */
@Service
public class SealDistributeService extends BaseService {
	
	@Autowired
	private VerifyConfigService configService;
	
	@Autowired
	private SealRecieveService sService;
	
	//印章下发不需要提示
//	@Task(code = "seal_distribution2", tip = "未下发的印章(@cnt)")
	public long selectDistributeList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='暂时停用' and t.flag='停用' and t.save_user_id=? ";

		Long cnt = super.getTaskCnt(sql,WebTools.getCurrentBankId(),WebTools.getCurrentUserId());
		
		return cnt;
	}
	
	//下发列表
	public List<Map<String, Object>> selectDistributeList(String bank_id,String srh_name,Page page){
		
		String sql="select t.*,tb.name bank_name,tn.title,tc.name config_name from t_seal t "
				+ " left join t_seal_new tn on t.seal_new_id=tn._id "
				+ "	left join t_seal_type_config tc on tn.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='暂时停用' and t.save_user_id=? "
				+ " and t.flag='停用' and t.true_code like ? order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(), page);
		return list;
	}

	//下发
	@Transactional
	public boolean distribute(String seal_id, t_seal_push push) {
		
		t_seal seal = super.findObjById(seal_id, t_seal.class);
		seal.setFlag("下发中");
		seal.setFlag_stop("下发停用");
		seal.setUse_bank_id(push.getBank_id());
		super.updateObj(seal);
		
		push.setDown_bank_id(push.getBank_id());
		push.setUser_id(WebTools.getCurrentUserId());
		push.setBank_id(WebTools.getCurrentBankId());
		super.saveObj(push);
		
		mySubmit(push);
		return true;
	}
	
	@Transactional
	private void mySubmit(t_seal_push push) {

		boolean isVerify = configService.isPushStart();
		if (isVerify == true) {
			t_check check = new t_check();
			check.setBank_id(push.getBank_id());
			check.setType("印章下发复核");
			check.setIs_verify("是");
			check.setVerify("待审核");
			check.setUser_id(WebTools.getCurrentUserId());
			super.saveObj(check);

			push.setCheck_id(check.get_id());
			push.setFlag("待复核");
			super.updateObj(push);
		}else{
			push.setFlag("通过");
			super.updateObj(push);
		}
	}
	
	@Task(code = "seal_distribution3", tip = "未完成的印章复核(@cnt)")
	public long selectDistributeCheckList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " join t_seal_push tp on tp.seal_id=t._id "
				+ " where t.status=0 and t.bank_id=? and t.save_user_id!=? and t.flag_stop='下发停用' and t.flag='下发中' and tp.flag='待复核' ";

		Long cnt = super.getTaskCnt(sql,WebTools.getCurrentBankId(),WebTools.getCurrentUserId());
		return cnt;
	}
	
	//复核列表
	public List<Map<String, Object>> selectDistributeCheckList(String bank_id,String srh_name,Page page){
	
		String sql="select t.*,tb.name bank_name,tbb.name down_bank_name,tn.title,tc.name config_name,tp._id push_id from t_seal t "
				+ " join t_seal_push tp on tp.seal_id=t._id "
				+ " left join t_bank tbb on tp.down_bank_id=tbb._id"
				+ " left join t_seal_new tn on t.seal_new_id=tn._id "
				+ "	left join t_seal_type_config tc on tn.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='下发停用' and t.save_user_id!=? "
				+ " and t.flag='下发中' and t.true_code like ? and tp.flag='待复核' order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(), page);
		return list;
	}

	//复核
	@Transactional
	public boolean check(String push_id, t_check check) {

		t_seal_push push = super.findObjById(push_id, t_seal_push.class);
		push.setFlag(check.getVerify());
		super.updateObj(push);
		
		t_check tc = super.findObjById(push.getCheck_id(), t_check.class);
		tc.setVerify(check.getVerify());
		tc.setVerify_bank_id(WebTools.getCurrentBankId());
		tc.setVerify_time(new Timestamp(System.currentTimeMillis()));
		tc.setVerify_user_id(WebTools.getCurrentUserId());
		tc.setNote(check.getNote());
		super.updateObj(tc);
		
		t_seal seal = super.findObjById(push.getSeal_id(), t_seal.class);
		if(!"通过".equals(push.getFlag())){
			seal.setFlag("停用");
			seal.setFlag_stop("暂时停用");
		}
		super.updateObj(seal);
		
		return true;
	}
	
	@Task(code = "seal_out", tip = "未完成的印章出库(@cnt)")
	public long selectDistributeOutList() {
		String sql = "select count(*) cnt from t_seal t "
				+ " join t_seal_push tp on tp.seal_id=t._id "
				+ " where t.status=0 and t.bank_id=? and tp.identifying_code is not null and t.save_user_id=? and t.flag_stop='下发停用' and t.flag='下发中' and tp.flag='通过' ";

		Long cnt = super.getTaskCnt(sql,WebTools.getCurrentBankId(),WebTools.getCurrentUserId());
		
		return cnt;
	}
	
	//出库列表
	public List<Map<String, Object>> selectDistributeOutList(String bank_id,String srh_name,Page page){
		
		String sql="select t.*,tb.name bank_name,tba.name down_bank_name,tn.title,tc.name config_name,tp._id push_id from t_seal t "
				+ " join t_seal_push tp on tp.seal_id=t._id "
				+ " left join t_seal_new tn on t.seal_new_id=tn._id "
				+ "	left join t_seal_type_config tc on tn.seal_type_config_id=tc._id "
				+ " left join t_bank tb on t.bank_id=tb._id "
				+ " left join t_bank tba on tp.down_bank_id=tba._id "
				+ " where t.status=0 and t.bank_id=? and t.flag_stop='下发停用' and t.save_user_id=? "
				+ " and t.flag='下发中' and t.true_code like ? and tp.identifying_code is not null and tp.flag='通过' order by t.created asc";
		List<String> params = new ArrayList<String>();
		params.add(bank_id);
		params.add(WebTools.getCurrentUserId());
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.true_code like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		List<Map<String, Object>> list = jc.queryForList(sql,params.toArray(), page);
		return list;
	}

	@Transactional
	public boolean out(String push_id, t_seal_push push,String msg) {
		
		t_seal_push p = super.findObjById(push_id, t_seal_push.class);
		p.setVerify_note(push.getVerify_note());
		p.setVerify_time(new Timestamp(System.currentTimeMillis()));
		if("giveup".equals(msg)){
			p.setFlag("不通过");
		}else{
			p.setFlag("在途");
		}
		super.updateObj(p);
		
		t_seal seal = super.findObjById(p.getSeal_id(), t_seal.class);
		if(!"giveup".equals(msg)){
			seal.setBank_id(p.getDown_bank_id());
		}else{
			seal.setFlag("印章出错");
			super.updateObj(seal);
			
			sService.errorCheck(p);
		}
		super.updateObj(seal);
		
		return true;
	}
	
	
}
